package com.youlai.mrp.controller;

import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.extra.spring.SpringUtil;
import com.youlai.common.base.BaseController;
import com.youlai.common.model.Option;
import com.youlai.common.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
import com.youlai.mrp.query.BaseQuery;
import com.youlai.mrp.query.PageQuery;
import com.youlai.mrp.entity.MrpTableEntity;
import com.youlai.mrp.service.MrpTableService;
import com.youlai.sale.entity.SaleCoEntity;
import com.youlai.sale.service.SaleCoService;
import com.youlai.system.constant.CacheConstants;
import com.youlai.utils.IdWorker;
import com.youlai.utils.ResponseUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Tag(name = "工程员工管理接口(jpa例子)")
@RestController
@RequestMapping("/api/v1/mrptable")
@RequiredArgsConstructor
public class MrpTableController extends BaseController<MrpTableEntity> {
    private final MrpTableService service;

    @Operation(summary = "runmrp")
    @PostMapping("/runmrp")
    public Result<MrpTableEntity> runMrp(@ParameterObject MrpTableEntity entity, HttpServletRequest request) {
        // 缓存中的mrp
        StringRedisTemplate redisTemplate = SpringUtil.getBean("stringRedisTemplate", StringRedisTemplate.class);
        String verifyCodeKey = request.getParameter("");
        String cacheMrp = redisTemplate.opsForValue().get(CacheConstants.MRP_RUN_PREFIX + "1");
        if (cacheMrp == null) {
            //设置过期时间，根据具体情况设置
            redisTemplate.opsForValue().set(CacheConstants.MRP_RUN_PREFIX + "1", null, 60l, TimeUnit.SECONDS);
        } else {
            // 验证码比对
            return Result.failed("MRP正在运行中，请等待完成");
        }

        Result<MrpTableEntity> result = service.runMrp(entity);
        redisTemplate.opsForValue().set(CacheConstants.MRP_RUN_PREFIX + "1", null, 1l, TimeUnit.SECONDS);
        return result;
    }

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    public PageResult<MrpTableEntity> getPage(@ParameterObject PageQuery queryParams) {
        PageResult<MrpTableEntity> page = service.getPageByHql(queryParams);
        return page;
    }

    @Operation(summary = "获取表单数据")
    @GetMapping("/{id}/form")
    public Result<MrpTableEntity> getForm(
            @Parameter(description ="字典ID") @PathVariable Long id
    ) {
        MrpTableEntity sysOrg = service.findById(id);
        return Result.success(sysOrg);
    }

    @Operation(summary = "新增")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sale:saleco:add')")
    @PreventDuplicateSubmit
    public Result insert(@RequestBody MrpTableEntity form) {
        service.save(form);
        return Result.judge(true);
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sale:saleco:edit')")
    public Result update(@PathVariable Long id, @RequestBody MrpTableEntity form) {
        form.setMrpTableId(id);
        service.save(form);
        return Result.judge(true);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sale:saleco:del')")
    public Result deleteByIds(@Parameter(description ="主键ID，多个以英文逗号(,)拼接") @PathVariable String ids) {
        boolean result = service.deleteAllById(ids);
        return Result.judge(result);
    }

    @Operation(summary = "销售客户下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listSaleCoOptions(@ParameterObject BaseQuery queryParams) {
        List<Option> list = service.listEngEmpOptions(queryParams);
        return Result.success(list);
    }
}
